rm(list = ls())
library(data.table)
library(plyr)
library(tidyr)
library(lfe)
library(stargazer)
library(xtable)
library(sandwich)
library(roll)
library(readxl)
library(readr)
library(zoo)
library(texreg)
library(DescTools)
library(ggplot2)

m <- data.table(read_xlsx("../Data/MasterData.xlsx", skip = 1, guess_max = 1e4))
m[, age := 2021 - year]

################################################################
# Demographics
################################################################

## Figure 3
# age
ggplot(m[age >= 18], aes(x = age)) +
  geom_bar(color = "black", fill = "white") +
  ggtitle("Age") +
  xlab("") + theme_minimal()
ggsave("../Figures/pic-survey-age.pdf", width = 5, height = 5.5/8*5)

# experience
ggplot(m, aes(x = round(experience / 12))) +
  geom_bar(color = "black", fill = "white") +
  ggtitle("Experience") +
  xlab("") + theme_minimal()
ggsave("../Figures/pic-survey-experience.pdf", width = 5, height = 5.5/8*5)

# gender
names <- sort(unique(m$gender))
m[gender == names[1], gender := "Male"]
m[gender == names[2], gender := "Female"]
m[, v := factor(gender, levels = c("Male", "Female"))]
ggplot(m, aes(x = v)) +
  geom_bar(color = "black", fill = "white") +
  ggtitle("Gender") +
  xlab("") + theme_minimal()
ggsave("../Figures/pic-survey-gender.pdf", width = 5, height = 5.5/8*5)

# education
nlist <- sort(unique(m$education))
vlist <- c(
  "Informal", "Elementary", "Middle", "High or\n Vocational",
  "Junior\nCollege", "Bachelor", "Master or\n Above", "N/A"
)
m[, v := education]
for (i in 1:length(vlist)) {
  m[v == nlist[i], v := vlist[i]]
}
m[, v := factor(v, levels = vlist)]
m <- m[v != "N/A"]
ggplot(m, aes(x = v)) +
  geom_bar(color = "black", fill = "white") +
  ggtitle("Education") +
  xlab("") + theme_minimal()
ggsave("../Figures/pic-survey-education.pdf", width = 5, height = 5.5/8*5)

# wealth
nlist <- sort(unique(m$total_wealth))
vlist <- c(
  "<20K", "20K-\n100K", "100K-\n200K", "200K-\n500K", "500K-\n1M",
  "1M-\n2M", "2M-\n10M", ">10M", "N/A"
)
m[, v := total_wealth]
for (i in 1:length(vlist)) {
  m[v == nlist[i], v := vlist[i]]
}
m[, v := factor(v, levels = vlist)]
ggplot(m[v != "N/A"], aes(x = v)) +
  geom_bar(color = "black", fill = "white") +
  ggtitle("Wealth") +
  xlab("") + theme_minimal()
ggsave("../Figures/pic-survey-wealth.pdf", width = 5, height = 5.5/8*5)

# income
nlist <- sort(unique(m$total_income))
m[total_income == nlist[substr(nlist, 1, 1) == "B"][2], total_income := nlist[substr(nlist, 1, 1) == "B"][1]]
nlist <- sort(unique(m$total_income))
vlist <- c(
  "<20K", "20K-\n100K", "100K-\n200K", "200K-\n500K", "500K-\n1M",
  "1M-\n2M", "2M-\n10M", ">10M", "N/A"
)
m[, v := total_income]
for (i in 1:length(vlist)) {
  m[v == nlist[i], v := vlist[i]]
}
m[, v := factor(v, levels = vlist)]
ggplot(m[v != "N/A"], aes(x = v)) +
  geom_bar(color = "black", fill = "white") +
  ggtitle("Income") +
  xlab("") + theme_minimal()
ggsave("../Figures/pic-survey-income.pdf", width = 5, height = 5.5/8*5)
